package com.bayair.doctor.mvp;

import android.content.Intent;
import android.os.Bundle;

import com.bayair.doctor.base.component.BaseFragment;
import com.bayair.doctor.mvp.proxy.ProxyFragment;
import com.bayair.doctor.ui.login.LoginActivity;
import com.bayair.doctor.utils.ActivityStackManager;

import androidx.annotation.Nullable;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

/**
 * Author: tuyrt7
 * Date: 2019/9/29 11:47
 * Description:
 */
public abstract class BaseMVPFragment extends BaseFragment implements IView {

    private ProxyFragment mProxyFragment;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mProxyFragment = createProxyFragment();
        mProxyFragment.bindPresenter();
    }

    private ProxyFragment createProxyFragment() {
        if (mProxyFragment == null) {
            return new ProxyFragment(this);
        }
        return mProxyFragment;
    }

    @Override
    public void onDestroy() {
        if (mProxyFragment != null) {
            mProxyFragment.unbindPresenter();
        }
        super.onDestroy();
    }

    @Override
    public void onLoading() {
        showLoading();
    }

    @Override
    public void onComplete() {
        showComplete();
    }

    @Override
    public void onEmpty() {
        //showEmpty();//打开必须在xml中有HintLayout
    }

    @Override
    public void onError() {
        //showError();//打开必须在xml中有HintLayout
    }

    /**
     * token过期，去登录页面
     */
    @Override
    public void tokenExpire() {
        toast("登录过期，请重新登录");
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        ActivityStackManager.getInstance().finishAllActivities(LoginActivity.class);
    }

}
